const {database} = require("@common/config")

class codeService{
  async selectUser(phone){
    try {
      const statement = `SELECT * FROM wx_user WHERE phone = ?`
      const [result] = await database.promise().execute(statement,[phone])
      return result[0] ?? []
    } catch (err) {
      throw new Error(err)
    }
  }

  async createUser(phone,code){
    try {
      const statement = `INSERT INTO wx_user (phone,code) VALUES (?,?)`
      const [result] = await database.promise().execute(statement,[phone,code])
      return result
    } catch (err) {
      throw new Error(err)
    }
  }

  async updateCode(phone,md5Code){
    try {
      const statement = `UPDATE wx_user SET code = ? where phone = ? `
      const [result] = await database.promise().execute(statement,[md5Code,phone])
      return result
    } catch (error) {
      throw new Error(err)
    }
  }
}

module.exports = new codeService()